/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package domain.forumdata;

import domain.DomainController;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.logging.Level;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 * Container of available categories.
 */
public class Categories implements Serializable
{
    private ArrayList<Category> categories;

    /**
     * Default constructor. Initializes an empty list.
     */
    public Categories()
    {
        this.categories = new ArrayList<Category>();
    }

    /**
     * Creates and saves a new category.
     * @param name The name for the new category.
     */
    public void addCategory(String name)
    {
        this.addCategory(new Category(name));
    }

    /**
     * Creates and saves a new category.
     * @param cat The category object.
     */
    public void addCategory(Category cat)
    {
        if(!this.categories.contains(cat))
        {
            this.categories.add(cat);
            DomainController.getInstance().getLogger().log(Level.INFO, "Created new category \"{0}\"", cat.getName());
            DomainController.getInstance().loadCategoriesUI();
            DomainController.getInstance().saveCategories();
        }
        else
        {
            DomainController.getInstance().getLogger().log(Level.WARNING, "Abort: Category \"{0}\" already exists...", cat.getName());
            DomainController.getInstance().fireMessage("Category \"" + cat.getName() + "\" already exists", "Error");
        }
    }

    /**
     * Removes a category.
     * Triggers a save of categories.
     * @param cat The category object to remove.
     */
    public void removeCategory(Category cat)
    {
        this.categories.remove(cat);
        DomainController.getInstance().getLogger().log(Level.INFO, "Removed category \"{0}\"", cat.getName());
        DomainController.getInstance().loadCategoriesUI();
        DomainController.getInstance().saveCategories();
    }

    /**
     * Removes a category by name.
     * Triggers a save of categories.
     * @param name The name of the category to remove.
     */
    public void removeCategory(String name)
    {
        for(Category cat : this.categories)
        {
            if(cat.getName().equals(name))
            {
                this.removeCategory(cat);
                break;
            }
        }
    }

    /**
     * @return The list of available categories.
     */
    public ArrayList<Category> getCategories()
    {
        return categories;
    }

    /**
     * Tries to find a category by name.
     * @param name The name of the category.
     * @return The category object when found, null otherwise.
     */
    public Category getCategoryByName(String name)
    {
        for(Category cat : this.categories)
        {
            if(cat.getName().equals(name))
            {
                return cat;
            }
        }
        return null;
    }
}